package day04;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        Trader t1 = new Trader("Zhou", "Beijing");
        Trader t2 = new Trader("Liang", "Shanghai");
        Trader t3 = new Trader("Xiang", "Beijing");
        Trader t4 = new Trader("Zhou", "Shenzhen");
        Trader t5 = new Trader("Gu", "Beijing");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(t1, 2020, 300),
                new Transaction(t2, 2021, 1000),
                new Transaction(t2, 2021, 400),
                new Transaction(t2, 2022, 710),
                new Transaction(t3, 2021, 700),
                new Transaction(t4, 2019, 950),
                new Transaction(t5, 2024, 1010),
                new Transaction(t5, 2023, 600)
        );
        System.out.println("----------------1------------------");
        //1.第一种基本输出
        for (int i = 0; i < transactions.size(); i++) {
            if (transactions.get(i).getYear() == 2021) {
                System.out.println(transactions.get(i));
            }
        }
        System.out.println("----------------2-----------------");
        //2.第二种输出  列表的名称.stream().filter(自定义的名称 -> 自定义的名称.getYear()==2021).collect(Collectors.toList());
        List<Transaction> t = transactions.stream().filter(transaction -> transaction.getYear() == 2021).collect(Collectors.toList());
        for (Transaction transaction : t) {
            System.out.println(transaction);
        }
        System.out.println("----------------3-----------------");
        //笔记：利用map的嵌套去解决类的嵌套，然后再使用distinct去除重复就可以了
        List<String> cities = transactions.stream().map(Transaction::getTrader).map(Trader::getCity).distinct().collect(Collectors.toList());
        for (String city : cities) {
            System.out.println(city);
        }
        System.out.println("----------------4-----------------");
        List<String> names = transactions.stream().filter(transaction -> transaction.getTrader().getCity().equals("Beijing")).map(Transaction::getTrader).map(Trader::getName).sorted().distinct().collect(Collectors.toList());
        for (String name : names) {
            System.out.println(name);
        }
    }
}
